package com.zhao.controller;

import com.zhao.pojo.Purchase;
import com.zhao.service.Impl.PurchaseServiceImpl;
import com.zhao.vo.PurchaseVoOne;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/purchase")
public class PurchaseController {
    @Autowired
    PurchaseServiceImpl purchaseService;
    @RequestMapping("/toList")
    public String toList(Model model){
        List<Purchase> purchases = purchaseService.selectAllPurchase();
        model.addAttribute("purchase",purchases);
        return "purchase/list";
    }
    @RequestMapping("/toAdd/{id}")
    public String toAdd(@PathVariable("id") int id,Model model){
        if (id!=0){
            Purchase purchase = purchaseService.selectPurchaseById(id);
            model.addAttribute("purchase",purchase);
        }
        return "purchase/add";
    }
    @RequestMapping("/add")
    public String add(PurchaseVoOne purchaseVoOne){
        if (purchaseVoOne.getId()!=0){
            //更新
            purchaseService.updatePurchase(purchaseVoOne);
        }else {
            purchaseVoOne.setPurchaseTime(new Date());
            //新增
            purchaseService.addPurchase(purchaseVoOne);
        }
        return "redirect:/purchase/toList";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id){
        purchaseService.deletePurchase(id);
        return "redirect:/purchase/toList";
    }
}
